"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】验证操作用户是事件触发器的owner或是超级用户，有权限删除事件触发器
Create At   :   2024/07/11
Owner       :   sungang14
Description :
    预置条件：
        1.创建用户
        CREATE USER u_eventtrigger_0035_1 sysadmin PASSWORD 'Bigdata@123';
        2.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0035()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        3、创建事件触发器：
        CREATE EVENT TRIGGER test_eventtri1 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0035();
    测试步骤：
        1、切换用户，创建表
        \c - u_eventtrigger_0035_1
        create table t_eventtrigger_0035(a int);
Expect      : 
        1、表创建成功，触发事件触发器函数
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop function if exists  f_eventtrigger_0035();")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0035 CASCADE;")
        self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0035_1 CASCADE;")
        msg = self.comsh.execut_db_sql("CREATE USER u_eventtrigger_0035_1 PASSWORD 'Bigdata@123';")
        self.logger.info(msg)
        self.assertEqual("CREATE ROLE", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0035() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0035();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "u_eventtrigger_0035_1用户创建表:create table t_eventtrigger_0035(a int);")
        msg = self.comsh.execut_db_sql(
            "create table t_eventtrigger_0035(a int);",
            sql_type=f" -U u_eventtrigger_0035_1 -W Bigdata@123")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE TABLE', msg)
        self.assertIn(f'CREATE TABLE', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0035();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0035 CASCADE;",
                                       sql_type=f" -U u_eventtrigger_0035_1 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual('DROP TABLE', msg)
        # 删除用户
        msg = self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0035_1 CASCADE;")
        self.logger.info(msg)
        self.assertEqual('DROP ROLE', msg)

        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
